<?php

/**
 * This is the model class for table "tbl_achievement".
 *
 * The followings are the available columns in table 'tbl_achievement':
 * @property integer $id
 * @property integer $type_id
 * @property integer $user_id
 * @property integer $views
 * @property string $title
 * @property string $content
 *
 * The followings are the available model relations:
 * @property User $user
 * @property AchievementType $type
 */
class Achievement extends CActiveRecord
{
	const TYPE_XQHZ = 1;
	const TYPE_KYCG = 2;
	const TYPE_JXCG = 3;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Achievement the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_achievement';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('type_id, user_id, views, title, content', 'required'),
			array('type_id, user_id, views', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, type_id, user_id, views, title, content', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'type' => array(self::BELONGS_TO, 'AchievementType', 'type_id'),
		);
	}
    public function getTypeUrl()
    {
        if($this->type == 1)
        {
            return array('display/schoolenterprise');
        }
        elseif($this->type == 2)
        {
            return array('display/ scientificachievement');
        }
    }
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'type_id' => '成果类型',
			'user_id' => '创建用户',
			'views' => '点击',
			'title' => '成果标题',
			'content' => '内容',
		);
	}
    public function getTypeOptions()
    {
        $model = AchievementType::model()->findAll();
        $data = array();
        foreach($model as $elem)
        {
            $data[$elem->type_id] = $elem->title;
        }
        return $data;
    }

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('type_id',$this->type_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('views',$this->views);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('content',$this->content,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}
